#include "stdafx.h"
#include "tm_singleton.h"
#include "timemanager.h"

namespace Clock
{
	TimeManager *Singleton::ourTimeManager = 0;
	unsigned int Singleton::ourManagersInUse = 0;

	void Singleton::StartManager()
	{
		if(ourTimeManager == 0)
		{
			ourTimeManager = new TimeManager();
			ourTimeManager->InitDefaults();
		}
		ourManagersInUse++;
	}

	void Singleton::StopManager()
	{
		if(ourManagersInUse == 0)
		{
			delete ourTimeManager;
			ourTimeManager = 0;
			return;
		}
		ourManagersInUse--;
	}

	void Singleton::Create(const Clock::Clocks &aTimer)
	{
		ourTimeManager->CreateTimer(aTimer);
	}

	void Singleton::AttachTimer(const Clock::Clocks &aTimer)
	{
		ourTimeManager->AttachTimerToTimer(aTimer);
	}

	void Singleton::AttachTimer(const Clock::Clocks &aTimerToAttach, const Clock::Clocks &aTimerToAttachTo)
	{
		ourTimeManager->AttachTimerToTimer(aTimerToAttach, aTimerToAttachTo);
	}

	void Singleton::StartClock(const Clock::Clocks &aTimer)
	{
		ourTimeManager->SetClockPauseState(aTimer, false);
	}

	void Singleton::StopClock(const Clock::Clocks &aTimer)
	{
		ourTimeManager->SetClockPauseState(aTimer, true);
	}

	Time Singleton::Read(const Clock::ReadType &aReadType)
	{
		switch(aReadType)
		{
		case ReadElapsedTime:
			return ourTimeManager->GetElapsedTime();
		default:
			return ourTimeManager->GetTotalTime();
		}
	}

	Time Singleton::Read(const Clock::Clocks &aTimer, const Clock::ReadType &aReadType)
	{
		switch(aReadType)
		{
		case ReadElapsedTime:
			return ourTimeManager->GetElapsedTime(aTimer);
		default:
			return ourTimeManager->GetTotalTime(aTimer);
		}
	}

	void Singleton::Update()
	{
		ourTimeManager->Update();
	}
};
